--[[
Copyright (C) 2006  Matt Richard

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
]]

local addonEntryMethods = {}

function addonEntryMethods:SetAddon(id)
    self.id = id
    return self:Update()
end

function addonEntryMethods:Update()
    local id = self.id
    local name, title, notes, enabled, loadable, reason, security = GetAddOnInfo(id)
    self.addonName:SetText(title)
    self.status:SetText(reason)
    self.checkbox:SetChecked(enabled)

    self:UpdateHighlight()
end

function addonEntryMethods:UpdateHighlight()
    local entered,selected = self.entered, Overlord.selected[self.id]
    if entered or selected then
        if not entered then self:LockHighlight() end
        self.addonName:SetTextColor(1,1,1)
        self.status:SetTextColor(1,1,1)
        local c = entered and 1 or .4
        self.highlight:SetVertexColor(c,c,c)
    else
        self:UnlockHighlight()    
        self.addonName:SetTextColor(1, 0.8196, 0)
        self.status:SetTextColor(1, 0.8196, 0)
    end        
end

local addonEntryScripts = {}

function addonEntryScripts:OnEnter()
    self.entered = true
    self:UpdateHighlight()
end
    
function addonEntryScripts:OnLeave()
    self.entered = nil
    self:UpdateHighlight()
end
 
function addonEntryScripts:OnClick()
    if IsShiftKeyDown() then
        Overlord:GroupSelection(self.id)
    elseif IsControlKeyDown() then
        Overlord:ToggleSelected(self.id)
    else
        Overlord:ClearSelection()
        Overlord:SetSelected(self.id, true)
    end

end
    
local checkButtonOnClick = function(self)
    if self:GetChecked() then
        Overlord:EnableAddon(self:GetParent().id)
    else
        Overlord:DisableAddon(self:GetParent().id)
    end
end

function Overlord:CreateAddonEntry(parent)
    
    local b = CreateFrame("Button",nil,parent)
    b:SetWidth(505)
    b:SetHeight(18)

    local t = b:CreateTexture(nil,"HIGHLIGHT")
    t:SetTexture("Interface\\QuestFrame\\UI-QuestTitleHighlight")
    t:SetBlendMode("ADD")
    t:SetAllPoints(b)
    b:SetHighlightTexture(t)
    b.highlight = t     
     
    local checkbox = CreateFrame("CheckButton",nil,b,"UICheckButtonTemplate")
    checkbox:SetWidth(24)
    checkbox:SetHeight(24)
    checkbox:SetPoint("RIGHT",b,"LEFT",-1,0)
    checkbox:SetScript("OnClick",checkButtonOnClick)
    b.checkbox = checkbox    
       
    t = b:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    t:SetPoint("LEFT")
    t:SetWidth(300)
    t:SetJustifyH("LEFT")
    b.addonName = t
    
    t = b:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    t:SetPoint("LEFT", b.addonName, "RIGHT", 30)
    b.status = t
    
    for key,method in pairs(addonEntryMethods) do
        b[key] = method
    end
    
    for script,method in pairs(addonEntryScripts) do
        b:SetScript(script,method)
    end
    
    return b
end
